/**
 * 这个是基础的图标展示mixin 文件，里面是图标更新、显示的统一方法，
 * 使用的前提是 配置文件的格式得严格匹配
 *
 */
import { REQUEST_ADDRESS_LOCAL, REQUEST_ADDRESS } from '@/utils/constant';
import { getImgUrl } from '@/utils/util';
import darkDefaultIcon from '@/components/common/images/dark-default-icon.png';
import lightDefaultIcon from '@/components/common/images/light-default-icon.png';
export default {
  data() {
    return {
      requestAddress: '',
    };
  },
  created() {
    if (window.isLocalEnv) {
      this.requestAddress = REQUEST_ADDRESS_LOCAL;
    } else {
      this.requestAddress = REQUEST_ADDRESS;
    }
  },
  methods: {

    // 单数值
    updateImgUrlSingleValue() {
      const imgId = this.config.config.property.icon;
      let imgUrl;
      if (imgId) {
        imgUrl = getImgUrl(this.requestAddress, imgId);
      }
      if (!imgUrl) {
        // imgUrl = new URL(`../components/common/images/title_${this.componentStyle}_0.png`, import.meta.url).href
        imgUrl = new URL(`../components/common/images/content_${this.componentStyle}_1.png`, import.meta.url).href;
      }
      this.imgUrl = imgUrl;
    },

    // 多数值
    updateImgUrlMultiValue(configData) {
      configData &&
        configData.forEach(item => {
          const imgId = item.icon;
          item.imgUrl = getImgUrl(this.requestAddress, imgId);
        });
    },

    // 多数值带阈值
    updateImgUrlThreshold(data, item) {
      const imgId = item.icon;
      item.imgUrl = getImgUrl(this.requestAddress, imgId);
      data.threImgUrl = item.imgUrl;

      // 新图标控件兼容
      if (typeof imgId === 'object' && imgId.icon_id) {
        data.icon = JSON.parse(JSON.stringify(imgId));
      }
    },

    // 指标带图标
    updateImgUrlMetricsList() {
      const configData = this.config.config.property.data;
      configData.forEach(item => {
        const imgId = item.icon;
        item.imgUrl = getImgUrl(this.requestAddress, imgId);
        item.childItems.forEach(child => {
          child.imgUrl = getImgUrl(this.requestAddress, child.icon);
        });
      });
    },

    // 指标列表
    updateImgUrlHorizontalIndicator() {
      const groupData = this.config.config.property.group;
      groupData.forEach(item => {
        const imgId = item.icon;
        item.imgUrl = getImgUrl(this.requestAddress, imgId);
      });
    },

    // 分组带图标
    updateImgUrlGroupValue() {
      const configData = this.config.config.property.data;
      configData.forEach(item => {
        const imgId = item.icon;
        item.imgUrl = getImgUrl(this.requestAddress, imgId);

        item.childItem &&
          item.childItem.forEach(childItem => {
            const itemImgId = childItem.icon;
            childItem.imgUrl = getImgUrl(this.requestAddress, itemImgId);
          });
      });
    },

    // 环形图特殊处理方法
    returnChainImgUrl(iconSmall) {
      const icon = this.styleType === 'dark' ? darkDefaultIcon : lightDefaultIcon;
      return iconSmall ? getImgUrl(this.requestAddress, iconSmall) : icon;
    },

    // 分组带图标列表
    updateImgUrlGroupIconList() {
      const configData = this.config.config.property.group;
      configData.forEach(item => {
        const imgId = item.icon;
        item.imgUrl = getImgUrl(this.requestAddress, imgId);
      });
    },

    // 标签组件有分组
    updateImgUrlLabelValue(configData) {
      configData.forEach(item => {
        item.childItem &&
          item.childItem.forEach(childItem => {
            const itemImgId = childItem.icon;
            childItem.imgUrl = getImgUrl(this.requestAddress, itemImgId);
          });
      });
    },
  },
};
